using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BGMControl : MonoBehaviour
{
    static public BGMControl Instance
    {
        get
        {
            if (instance == null)
            {
                instance=FindObjectOfType<BGMControl>();
            }
            if(instance == null)
            {
                GameObject go = new GameObject("BGMSourceObject");
                DontDestroyOnLoad(go);
                instance = go.AddComponent<BGMControl>();
            }
            return instance;
        }
    }
    static BGMControl instance;
    public enum BGMMusics{
        You,
        }
    public BGMMusics music;
    AudioSource myAudioSource;
    Dictionary<string, AudioClip> audioSources;
    private void Awake()
    {
        
        if (FindObjectsOfType<BGMControl>().Length>1) Destroy(this.gameObject);
    }
    void Start()
    {
        audioSources = new Dictionary<string, AudioClip>();
        
        

        AssetBundle ab = ABManger.LoadFromFile("sounds");
        AudioClip[] list=ab.LoadAllAssets<AudioClip>();
        foreach (AudioClip clip in list)
        {
            audioSources.Add(clip.name, clip);
        }
        ABManger.UnLoad(ab, false,out ab);

        string str = music.ToString();
        myAudioSource = GetComponent<AudioSource>();
        myAudioSource.loop = true;
        myAudioSource.clip = audioSources[str];
        myAudioSource.Play();

        DontDestroyOnLoad(gameObject);
    }

    
}
